<?php

/*
	Function createthumb($name,$filename,$new_w,$new_h)
	creates a resized image
	variables:
	$name		Original filename
	$filename	Filename of the resized image
	$new_w		width of resized image
	$new_h		height of resized image
*/	
function createthumb($name,$filename,$new_w,$new_h)
{
	
	if (preg_match("/\.(jpg|jpeg)$/",$name)){$src_img=imagecreatefromjpeg($name);}
	elseif (preg_match("/\.png$/",$name)){$src_img=imagecreatefrompng($name);}
	elseif (preg_match("/\.gif$/",$name)){$src_img=imagecreatefromgif($name);}
	else return false;
	
	
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	if ($old_x > $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y*($new_h/$old_x);
	}
	if ($old_x < $old_y) 
	{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) 
	{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

	if (preg_match("/\.png$/",$name))
	{
		imagepng($dst_img,$filename); 
	} elseif (preg_match("/\.gif$/",$name)){
		imagegif($dst_img,$filename); 
	} else {
		imagejpeg($dst_img,$filename);
		echo $name;
		echo $filename; 
	}
	
	imagedestroy($dst_img); 
	imagedestroy($src_img); 
}









?>